package com.example.hamilyjing.androiddemo.module.main;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hamilyjing.androiddemo.module.main.presenter.JJMainActivityPresenter;
import com.example.hamilyjing.androiddemo.service.homepageService.JJHomepageService;
import com.example.hamilyjing.androiddemo.service.homepageService.model.JJHomepageModel;
import com.mobileClientKernel.android.commonLayer.FileOperator;
import com.mobileClientKernel.android.serviceLayer.httpTransportService.urlconfig.JJTUrlConfigManager;
import com.mobileClientKernel.android.serviceManager.network.JJRequestError;
import com.mobileClientKernel.android.serviceManager.service.IJJServiceCallBack;
import com.mobileClientKernel.android.serviceManager.servicefactory.JJServiceFactory;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {

    public JJMainActivityPresenter getMainActivityPresenter() {
        return mainActivityPresenter;
    }

    public JJMainActivityPresenter mainActivityPresenter = new JJMainActivityPresenter();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        JJServiceFactory.getInstance().setContext(getApplicationContext());

        String jsonString = FileOperator.readAssetFile(getApplicationContext(), "JJUrlConfig.json");
        try {
            JSONObject urlConfigObject = new JSONObject(jsonString);
            JJTUrlConfigManager.getInstance().addUrlConfig(urlConfigObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        final MainActivity me = this;

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                me.getMainActivityPresenter().requestGetWeather(getApplicationContext(), new IJJServiceCallBack() {
                    @Override
                    public void onNetworkRequestSuccess(Object model, JSONObject otherInfo) {
                        JJHomepageModel homepageModel = (JJHomepageModel)model;
                        if (homepageModel.isValid()) {
                            TextView textView = findViewById(R.id.textview);
                            textView.setText(me.getMainActivityPresenter().getErrMsg());
                        }
                    }

                    @Override
                    public void onNetworkRequestFailure(JJRequestError error, JSONObject otherInfo) {
                        Toast.makeText(MainActivity.this, "network error", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
}
